package com.xugou.controller;

import com.sun.org.apache.xpath.internal.operations.Or;
import com.xugou.pojo.Client;
import com.xugou.pojo.Dept;
import com.xugou.pojo.Order;
import com.xugou.service.ClientService;
import com.xugou.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xugou
 * @date 2022/6/13  16:30
 */
@Controller
@RequestMapping("/client")
public class ClientController {

    @Autowired
    @Qualifier("clientServiceImpl")
    private ClientService clientService;

    @Autowired
    @Qualifier("orderServiceImpl")
    private OrderService orderService;

    //获取所有客户信息
    @RequestMapping("/alldept")
    public String getAllDeptInfo(HttpServletRequest req) {

        List<Client> clientList = clientService.getClientList();
        if (clientList != null) {
            req.setAttribute("clientList", clientList);
            return "client/client_index";
        } else {
            return null;
        }

    }

    //删除客户信息
    @RequestMapping("/delClient/{ClientId}")
    public String delClientInfo (@PathVariable("ClientId") int id,HttpServletRequest req){

        clientService.deleteClientById(id);
        return "redirect:/client/alldept";



    /*    //判断该用户是否有订单
        System.out.println("===========");
        System.out.println(id);
        List<Order> orderList = orderService.getOrderInfoByClientId(id);
        if (orderList==null){
            //无，去删
            clientService.deleteClientById(id);
            return "redirect:/client/alldept";
        }else{
            //有，不可删除，返回错误信息
            String msg = "";
            msg="删除失败，因为还有该客户的订单";
            req.setAttribute("msg",msg);
            return "redirect:/client/alldept";
        }

     */
    }

    //    跳转到修改界面
    @RequestMapping("/toUpdateClient/{ClientId}")
    public String toUpdateClient(@PathVariable("ClientId") int id, Model model){
        Client client = clientService.queryClientById(id);
        model.addAttribute(client);
        return "/client/updateClient";
    }

    //    修改用户信息
    @RequestMapping("/updateClient/{ClientId}")
    public String updateClient(Client client,@PathVariable("ClientId") int id){

        client.setClientId(id);
        boolean b = clientService.updateClient(client);
        if(b){
            return "redirect:/client/alldept";
        }else {
            return "redirect:/client/updateClient";
        }
    }


    //添加用户
    @RequestMapping("/addClient")
    public String addClient(Client client){

        Object max ;
        List<Client> clientList = clientService.getClientList();
        ArrayList list = new ArrayList();
        for(int i = 0 ; i<clientList.size();i++){
            list.add(clientList.get(i).getClientId());
        }
        max = list.get(list.size()-1);
        int i = Integer.parseInt(String.valueOf(max));
        i++;

        client.setClientId(i);
        boolean b = clientService.addClientInfo(client);
        System.out.println(client);
        if (b){
            return "redirect:/client/alldept";
        }else {
            return "client/toAddClient";
        }
    }


}
